package linuxbox.filelib.bean;

import java.util.*;

import org.apache.log4j.*;
import linuxbox.bean.*;
import linuxbox.filelib.logic.*;

public class PermissionEntryBean extends BaseBean
{
  protected static Logger logger = Logger.getLogger(PermissionEntryBean.class.getName());

  //each BaseBean derived class should define this
  static protected BeanDef beanDef;

  protected BeanDef getBeanDef()
  {
    return beanDef;
  }

  public PermissionEntryBean()
  {
  }

  protected void setupFields() throws Exception
  {
    if (beanDef == null)
    {
      //Set the table name and the data object associated with the table
      beanDef = new BeanDef("filelib_permissions", PermissionEntry.class);

      //Setup the fields for the db object
      //name, fieldType, isKey, allowNull, defaultValue, description
      DBField field = beanDef.addKeyField("id", DBField.INT_TYPE, false, "1", "");
      field.setAutoIncrement(true);

      beanDef.addField("objectid", DBField.INT_TYPE, false, "0", "");
      beanDef.addField("userid", DBField.INT_TYPE, false, "0", "");
      beanDef.addField("permission", DBField.LONG_TYPE, false, "0", "");
      beanDef.addField("isgroup", DBField.BOOLEAN_TYPE, false, "0", "");
/*
      id  int4 not null,
      objectid int4 not null,
      userid int4 not null,
      isgroup int2 not null,
      permission int8 not null,
*/
    }
  }

  /**
   * getPermissionList(int objid)
   *
   * @return Collection of permission entries. Sorted by isgroup flag
   * will check single user's permission first then group
   */
  public Collection getPermissionList(int objid) throws Exception
  {
    StringBuffer sql = new StringBuffer();
    sql.append("objectid=");
    sql.append(objid);
    sql.append(" order by isgroup asc ");

logger.error("BBBBBBB before findBySql");
    return findBySql(sql.toString());
  }

  /**
   * updatePermissions
   *
   * @param objectID int
   * @param permissions String[]
   * @return boolean
   */
  public boolean updatePermissions(int objectID, String[] permissions) throws Exception
  {
    //clear the permissions
    String sql = "DELETE FROM " + getTableName() + " where objectid=" + objectID;
    updateBySql(sql);

    if ( permissions != null )
    {
      //userID(?) + GroupFlag(1) + permissions(8)(BROWSE/READ/WRITE/TAKEOWNERSHIP)(ALLOW/DENY)
      for(int i=0; i < permissions.length; i++)
      {
        PermissionEntryBean permissionBean = new PermissionEntryBean();
        permissionBean.create();

        PermissionEntry entry = (PermissionEntry)permissionBean.getObject();
        entry.setObjectid(objectID);
        String tmp = permissions[i].substring(permissions[i].length()-8);
        entry.setPermission(Long.parseLong(tmp, 2));
        tmp = permissions[i].substring(0,permissions[i].length()-9);
        entry.setUserid(Integer.parseInt(tmp));
        entry.setIsgroup(permissions[i].charAt(permissions[i].length()-9) == '1');
        permissionBean.store();
      }
    }
    return true;
  }

}
